<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./jquery.min.js"></script>
</head>

<body>
    <input type="file" id="file1" />
    <button id="btnUpload">上传文件</button>
    <script>
        $(function() {
            // 执行Ajaxstart函数，可以在回调函数中娴熟loading效果
            $(document).ajaxStart(function() {
                $('#loading').show()
            });

            // 监听到ajax完成事件 隐藏loading
            $(document).ajaxStop(function() {
                $('#loading').hide()
            })

            $('#btnUpload').on('click', function() {
                var files = $('#file1')[0].files
                if (files.length <= 0) {
                    return alert('请选择文件后上传！')
                }
                var fd = new FormData()
                fd.append('avatar', files[0])
                    //  发起jQuery的ajax请求。上传文件
                $.ajax({
                    method: 'POST',
                    url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                    data: fd,
                    processData: false,
                    contentType: false,
                    success: function(res) {
                        console.log(res);
                    }
                })
            })
        })
    </script>
</body>

</html>